/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.entities.RawProduct;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import kimo.persistence.controllers.exceptions.IllegalOrphanException;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.RawProductType;

/**
 *
 * @author a381u296
 */
public class RawProductTypeJpaController implements Serializable {

    public RawProductTypeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(RawProductType rawProductType) {
        if (rawProductType.getRawProductList() == null) {
            rawProductType.setRawProductList(new ArrayList<RawProduct>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<RawProduct> attachedRawProductList = new ArrayList<RawProduct>();
            for (RawProduct rawProductListRawProductToAttach : rawProductType.getRawProductList()) {
                rawProductListRawProductToAttach = em.getReference(rawProductListRawProductToAttach.getClass(), rawProductListRawProductToAttach.getRawProductId());
                attachedRawProductList.add(rawProductListRawProductToAttach);
            }
            rawProductType.setRawProductList(attachedRawProductList);
            em.persist(rawProductType);
            for (RawProduct rawProductListRawProduct : rawProductType.getRawProductList()) {
                RawProductType oldRawProductTypeIdOfRawProductListRawProduct = rawProductListRawProduct.getRawProductTypeId();
                rawProductListRawProduct.setRawProductTypeId(rawProductType);
                rawProductListRawProduct = em.merge(rawProductListRawProduct);
                if (oldRawProductTypeIdOfRawProductListRawProduct != null) {
                    oldRawProductTypeIdOfRawProductListRawProduct.getRawProductList().remove(rawProductListRawProduct);
                    oldRawProductTypeIdOfRawProductListRawProduct = em.merge(oldRawProductTypeIdOfRawProductListRawProduct);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RawProductType rawProductType) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProductType persistentRawProductType = em.find(RawProductType.class, rawProductType.getRawProductTypeId());
            List<RawProduct> rawProductListOld = persistentRawProductType.getRawProductList();
            List<RawProduct> rawProductListNew = rawProductType.getRawProductList();
            List<String> illegalOrphanMessages = null;
            for (RawProduct rawProductListOldRawProduct : rawProductListOld) {
                if (!rawProductListNew.contains(rawProductListOldRawProduct)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain RawProduct " + rawProductListOldRawProduct + " since its rawProductTypeId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<RawProduct> attachedRawProductListNew = new ArrayList<RawProduct>();
            for (RawProduct rawProductListNewRawProductToAttach : rawProductListNew) {
                rawProductListNewRawProductToAttach = em.getReference(rawProductListNewRawProductToAttach.getClass(), rawProductListNewRawProductToAttach.getRawProductId());
                attachedRawProductListNew.add(rawProductListNewRawProductToAttach);
            }
            rawProductListNew = attachedRawProductListNew;
            rawProductType.setRawProductList(rawProductListNew);
            rawProductType = em.merge(rawProductType);
            for (RawProduct rawProductListNewRawProduct : rawProductListNew) {
                if (!rawProductListOld.contains(rawProductListNewRawProduct)) {
                    RawProductType oldRawProductTypeIdOfRawProductListNewRawProduct = rawProductListNewRawProduct.getRawProductTypeId();
                    rawProductListNewRawProduct.setRawProductTypeId(rawProductType);
                    rawProductListNewRawProduct = em.merge(rawProductListNewRawProduct);
                    if (oldRawProductTypeIdOfRawProductListNewRawProduct != null && !oldRawProductTypeIdOfRawProductListNewRawProduct.equals(rawProductType)) {
                        oldRawProductTypeIdOfRawProductListNewRawProduct.getRawProductList().remove(rawProductListNewRawProduct);
                        oldRawProductTypeIdOfRawProductListNewRawProduct = em.merge(oldRawProductTypeIdOfRawProductListNewRawProduct);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = rawProductType.getRawProductTypeId();
                if (findRawProductType(id) == null) {
                    throw new NonexistentEntityException("The rawProductType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProductType rawProductType;
            try {
                rawProductType = em.getReference(RawProductType.class, id);
                rawProductType.getRawProductTypeId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The rawProductType with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<RawProduct> rawProductListOrphanCheck = rawProductType.getRawProductList();
            for (RawProduct rawProductListOrphanCheckRawProduct : rawProductListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This RawProductType (" + rawProductType + ") cannot be destroyed since the RawProduct " + rawProductListOrphanCheckRawProduct + " in its rawProductList field has a non-nullable rawProductTypeId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(rawProductType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RawProductType> findRawProductTypeEntities() {
        return findRawProductTypeEntities(true, -1, -1);
    }

    public List<RawProductType> findRawProductTypeEntities(int maxResults, int firstResult) {
        return findRawProductTypeEntities(false, maxResults, firstResult);
    }

    private List<RawProductType> findRawProductTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RawProductType.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RawProductType findRawProductType(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RawProductType.class, id);
        } finally {
            em.close();
        }
    }

    public int getRawProductTypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RawProductType> rt = cq.from(RawProductType.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
